﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kalficom.Forms
{
    public partial class frmUser_CreateModify : Form
    {
        public frmUser_CreateModify()
        {
            InitializeComponent();
            conf.Icon(this);
        }

        Libraies.db db = new Libraies.db();
        Query.qry_User query = new Query.qry_User();
        Libraies.msg msg = new Libraies.msg();

        private void btnOk_Click(object sender, EventArgs e)
        {
            // Check Identifiation blank field
            if (txtIdentification.Text.Trim() == "")
            {
                MessageBox.Show(msg.user_identificationBlankMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtIdentification.Focus();
                return;
            }

            // Check Password blank field.
            if (txtPassword.Text.Trim() == "" || txtConfirmPassword.Text.Trim() == "")
            {
                MessageBox.Show(msg.user_passwordBlankMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }

            // Check Password and Confirm password are match field.
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show(msg.user_passwordConfirmMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }

            DataRow row = tblUser.NewRow();
            row["user_login"] = txtIdentification.Text;
            row["user_pwd"] = txtPassword.Text;
            row["user_first_name"] = txtFirstName.Text;
            row["user_last_name"] = txtLast_Name.Text;
            row["user_mail"] = txtMail.Text;
            row["user_phone"] = txtPhone.Text;
            row["user_is_admin"] = chkAdministrator.Checked;
            row["user_is_tablet"] = chkCanusetable.Checked;
            row["user_can_create_contract"] = chkContractcreation.Checked;
            row["user_can_update_calypso"] = chkUpdatetocalypso.Checked;
            row["user_create"] = conf.CurUser;
            row["date_create"] = db.GetDate();

            // If form's Tag is null -> create new user.
            // If from's Tag not null -> modify user. (Tag is user_id for modify)
            if (this.Tag == null)
            {

                // Check Identifiation dublicate field.
                if (db.ExecuteScalar(string.Format(query.scalarID, db.SqlString(txtIdentification.Text))) != "")
                {
                    MessageBox.Show(msg.user_identificationExistMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtIdentification.Focus();
                    return;
                }

                if (MessageBox.Show(msg.saveMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Add user into database.
                    tblUser.Rows.Add(row);

                    db.CommandBuilder(query.get, tblUser);

                    // Close the form and return dialog result OK.
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            else
            {
                // Check Identifiation dublicate field.
                string user_id = db.ExecuteScalar(string.Format(query.scalarID, db.SqlString(txtIdentification.Text)));
                if (user_id != "" && user_id != tblUser.Rows[0]["user_id"].ToString())
                {
                    MessageBox.Show(msg.user_identificationExistMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtIdentification.Focus();
                    return;
                }

                if (MessageBox.Show(msg.saveMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Update user into database.
                    row["user_id"] = this.Tag;
                    tblUser.Rows[0].ItemArray = row.ItemArray;
                    
                    db.CommandBuilder(string.Format(query.get_where, this.Tag), tblUser);

                    // Close the form and return dialog result OK.
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }

            }
        }

        private void frmUser_CreateModify_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                db.FillDataSet(string.Format(query.get_where, this.Tag), tblUser);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
